import httplib,urllib,HTMLParser
def visit(addr):
	tempIndex=addr.rfind('/')
	url=addr
	page='/'
	if(tempIndex != -1):	
		url=addr[0:tempIndex]
		page=addr[tempIndex:]
	print 'URL is '+url+ ' \nPage is '+page
	websit = httplib.HTTPConnection(url)
	response=websit.request('GET',page)
	return websit.getresponse().read()

ID='#'
CLASS='.'
CHL='>'
SUB='~'
NEX='+'
AFT=' '

class HTMLObject():
	def __init__(self):
		self.body={}
		# not sure about js/css need to record
		# self.head={}
		# self.script=[]
		# self.css=[]

	def parse(self,content):
		# head_begin=content.find('<head>')
		# head_end=content.find('</head>')+7
		# print content[head_begin:head_end]	
		body_begin=content.find('<body>')+7
		body_end=content.find('</body>')
		body_content=content[body_begin:body_end]
		# print body_content
		self.parseValue(body_content,0)
		# tag_begin=body_content.index('<')+1
		# tag_end=body_content.index('>')
		# tag=body_content[tag_begin:tag_end].split(' ')
		# self.body=self.parseTag(tag)
		# print self.body

	def parseTag(self,tags,parent=''):
		tag=tags.split(' ')
		print tag
		tag_dict={}
		if parent == '':
			parent=self.body
		if not parent.has_key(tag[0]):
			parent[tag[0]]=[]
		for attrs in tag:
			if attrs != tag[0]:
				attr=attrs.split('=')
				tag_dict[attr[0]]=attr[1]
		parent[tag[0]].append(tag_dict)
		return parent

	def parseValue(self,content,idx):
		tag_dict={}
		tag_begin=content.index('<',idx)+1
		tag_end=content.index('>',idx)
		txt_idx=tag_end
		tag=content[tag_begin:tag_end]
		tag_dict=self.parseTag(tag)
		temp_val=''
		while content[txt_idx] != None:
			if content[txt_idx] == '<':
				temp_dict,txt_idx=self.parseValue(content,txt_idx)
				tag_dict['chlidren'].append(temp_dict)
			else:
				temp_val+=content[txt_idx]
			txt_idx+=1
		print temp_val
		return (tag_dict,idx)
#test data 'div#OSC_Channels ul li a.home'
#tag:{value:"",chlidren:[],attr:[{},{}]}
test_tag='div#OSC_Channels ul li a.home'

def query(content,tag):
	tags=tag.split(' ')
	html=HTMLObject()
	html.parse(content)


if __name__ == '__main__':
	content=visit('www.oschina.net')
	# print content
	query(content,test_tag)
